package ${package}.${moduleName}.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import ${package}.${moduleName}.converter.${converterClassName}Converter;
import ${package}.${moduleName}.mapper.${daoClassName}Mapper;
import ${package}.${moduleName}.service.${serviceClassName}Service;
import ${package}.${moduleName}.domain.vo.${ClassName}VO;
import ${package}.${moduleName}.domain.form.${ClassName}Form;
import ${package}.${moduleName}.domain.query.${ClassName}Query;
import ${package}.${moduleName}.domain.entity.${ClassName}Entity;
import ${package}.mybatisplus.domain.vo.BasePageVO;

/**
 * ${tableComment}
 *
 * @author ${author}
 * @datetime ${datetime}
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ${ClassName}ServiceImpl extends ServiceImpl<${daoClassName}Mapper, ${ClassName}Entity> implements ${serviceClassName}Service {

    private final ${serviceClassName}Converter ${className}Converter;

    @Override
    public BasePageVO<${ClassName}VO> page(${ClassName}Query ${className}Query) {
        // 分页数据
        Page<${ClassName}Entity> page = lambdaQuery()
            .page(new Page<>(${className}Query.getPageNum(), ${className}Query.getPageSize()));
        // entity -> vo
        Page<${ClassName}VO> voPage = ${className}Converter.entity2VO(page);

        return BasePageVO.success(voPage);
    }

    @Override
    public boolean save(${ClassName}Form ${className}Form) {
        // form -> entity
        ${ClassName}Entity entity = ${className}Converter.form2Entity(${className}Form);
        return save(entity);
    }

    @Override
    public boolean update(${ClassName}Form ${className}Form){
        // 根据 ID 查询查询实体
        ${ClassName}Entity entity = getById(${className}Form.getId());
        if (Objects.isNull(entity)) {
            log.warn("实体不存在");
        }
        // form -> entity
        ${className}Converter.form2Entity(${className}Form);
        return updateById(entity);
    }

    <#-- 只有设置了主键类型, 才会生成 delete 方法 -->
    <#if pkType??>
    @Override
    public boolean deleteByIds(String ids) {
        List<${pkType}> idList = Arrays.stream(ids.split(",")).map(${pkType}::parse${pkType}).toList();
        return removeByIds(idList);
    }

    @Override
    public ${ClassName}VO getInfoById(${pkType} id) {
        ${ClassName}Entity ${className}Entity = getById(id);
        if (Objects.isNull(${className}Entity)) {
            log.warn("实体信息不存在");
        }
        return ${className}Converter.entity2VO(${className}Entity);
    }
    </#if>

}